class Solution:
    def minEnd(self, n: int, x: int) -> int:
        n, j, ans = n - 1, 0, x
        for i in range(32):
            if x >> i & 1 == 0:
                ans |= ((n >> j) & 1) << i
                j += 1
        return ans
